package com.xx.eshop.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xx.eshop.entity.Attr;
import com.xx.eshop.entity.Category;
import com.xx.eshop.service.AttrService;
import com.xx.eshop.service.CategoryService;

@Controller
public class AttrController {
	
	@Autowired
	private AttrService attrService;
	
	@Autowired
	private CategoryService categoryService;
	
	
	// 修改属性
	@RequestMapping("/admin/attr/attrUpdate")
	// requestBody 把请求参数(json类型)的转换成java类型
	public String attrUpdate(@RequestBody List<Attr> attrList){
		//System.out.println("---------------------------------------"+attrList.size());
		for (int i = 0; i < attrList.size(); i++) {
			//System.out.println("================"+attrList.get(i).getName());
			Attr attr = (Attr) attrList.get(i);
			attrService.update(attr);
		}
		return "redirect:/admin/attr/findAll";
	}
	
	// 删除属性    连同属性值一起删除
	@RequestMapping("/admin/attr/delete")
	public String delete(int id){
		System.out.println("-=---=--id----"+id);
		attrService.delete(id);
		return "redirect:/admin/attr/findByCategory";
	}
	
	// 新增属性
	@RequestMapping("/admin/attr/save")
	public String save(Attr attr){
		System.out.println("---------attr save---------------");
		System.out.println("---------attr save---------------"+attr.getName());
		attrService.save(attr);
		return "redirect:/admin/attr/findAll";
	}
	
	
	@RequestMapping("/admin/attr/index")
	public String index() {
		return "/admin/attr/index";
	}
	
	@RequestMapping("/admin/attr/findByCategory")
	@ResponseBody
	public Map<String,Object> findByCategory(Integer cid) {
		Map<String, Object> map = new HashMap<>();	
		if(cid == null) {
			map.put("rows", Collections.emptyList());
		} else {
			map.put("rows", attrService.findByCategory(cid));
		}
		return map;
	}
	
	@RequestMapping("/admin/value/findByAttr")
	@ResponseBody
	public Map<String,Object> findByAttr(Integer aid) {
		Map<String, Object> map = new HashMap<>();	
		if(aid == null) {
			map.put("rows", Collections.emptyList());
		} else {
			map.put("rows", attrService.findByAttr(aid));
		}
		return map;
	}
	
	@RequestMapping("/admin/attr/findAll")
	@ResponseBody
	public List<Category> findAll() {
		//System.out.println("进入了/admin/attr/findAll");
		//System.out.println("进入了/admin/attr/findAll"+categoryService.findAll().size());
		return categoryService.findAll();
	}
	
	@RequestMapping("/admin/attr/findByCategoryFetch")
	@ResponseBody
	public Map<String,Object> findByCategoryFetch(Integer cid) {
		Map<String, Object> map = new HashMap<>();	
		if(cid == null) {
			map.put("rows", Collections.emptyList());
		} else {
			map.put("rows", attrService.findByCategoryFetch(cid));
		}
		return map;
	}
	
	/*@RequestMapping("/admin/attr/attrSort")
	public String attrSort(@RequestParam("attrId")List<Integer> attrId,@RequestParam("attrSort")List<Integer> attrSort){
		//System.out.println("----------");
		for (int i = 0; i < attrId.size(); i++) {
			// 下标遍历
			attrService.updateSort(attrId.get(i),  attrSort.get(i));
		}
		return "redirect:/admin/attr/findAll";
	}*/

}
